package net.sf.midollo.net.server;

import java.io.IOException;

import javax.microedition.io.StreamConnection;

import net.sf.midollo.io.IOStream;
import net.sf.midollo.net.io.StreamConnectionIOStream;
import net.sf.midollo.net.protocol.ProtocolFactory;
import net.sf.midollo.net.protocol.ProtocolRunner;

public class SyncAcceptHandler implements AcceptHandler {

  protected final ProtocolFactory protocolFactory;
  protected IOStream ioRequest;

  public SyncAcceptHandler(ProtocolFactory protocolFactory) {
    this.protocolFactory = protocolFactory;
  }

  public void accept(StreamConnection sc) throws IOException {
    this.ioRequest = new StreamConnectionIOStream(sc);
    ProtocolRunner protocolRunner = new ProtocolRunner(this.ioRequest, this.protocolFactory);
    protocolRunner.runProtocol();
  }

  public void release() throws IOException {
    if (this.ioRequest != null)
      this.ioRequest.close();
  }
}
